package at.technikumwien.trackmydevice.at.technikumwien.trackmydevice;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.SeekBar;
import at.technikumwien.trackmydevice.R;

public class startup extends Activity {
	public int MAX_INTERVAL = 10; //10 steps
	public String Step_0 = "10 Seconds";
	public String Step_1 = "30 Seconds";
	public String Step_2 = "1 Minute";
	public String Step_3 = "5 Minutes";
	public String Step_4 = "10 Minutes";
	public String Step_5 = "15 Minutes";
	public String Step_6 = "20 Minutes";
	public String Step_7 = "25 Minutes";
	public String Step_8 = "30 Minutes";
	public String Step_9 = "1 hour";
	public int interval_loc = 0; //store the update intervall
	public boolean use_map = false;
	Intent serviceIntent = null;
	Intent mapViewIntent = null;
	Context alertContext = null;
	AlertDialog.Builder Infodialog;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //IntentCreation
        serviceIntent = new Intent(this, TrackingService.class);
        mapViewIntent = new Intent(this, mapview.class);
        setContentView(R.layout.main_layout);
        alertContext = this;
        //UI Elements initialization
        final TextView txt_interval = (TextView) findViewById(R.id.textView3);
        Button btn_showMap = (Button) findViewById(R.id.btn_start);
        Button btn_closeApp = (Button) findViewById(R.id.btn_Exit);
        final ToggleButton btn_useMap = (ToggleButton) findViewById(R.id.tB_showMap);
        SeekBar prgBar_chooseInterval = (SeekBar) findViewById(R.id.sB_Interval);
        prgBar_chooseInterval.setBackgroundColor(Color.BLUE);
        prgBar_chooseInterval.setHapticFeedbackEnabled(true);
        prgBar_chooseInterval.setMax(MAX_INTERVAL);
        
        final AlertDialog.Builder Infodialog = new AlertDialog.Builder(alertContext);
        //Infodialog
        Infodialog.setMessage("This will start a background process. It is necessary to start this process due to the purpose of this application. You can stop this background process at any time by clicking stop in your status bar.");
        Infodialog.setCancelable(false);
        Infodialog.setTitle("Important:");
        Infodialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface Infodialog, int id) {
	        	   handleButtonClick();
	           }
	       });
        Infodialog.setNegativeButton("Abort", new DialogInterface.OnClickListener() {
			  public void onClick(DialogInterface Infodialog, int id) {
				  	Infodialog.cancel();
			  }
        });
        //btn_useMap OnClickListener
	    btn_useMap.setOnClickListener(new View.OnClickListener() 
	    {
	    	public void onClick(View v) {
				// TODO Auto-generated method stub
	    		use_map = btn_useMap.isChecked();
			}
		});
	    //SeekBar onChangeListener
	    prgBar_chooseInterval.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
	    {
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				switch(interval_loc)
				{
				case 0:	txt_interval.setText(Step_0); break;
				case 1:	txt_interval.setText(Step_1); break;
				case 2:	txt_interval.setText(Step_2); break;
				case 3:	txt_interval.setText(Step_3); break;
				case 4:	txt_interval.setText(Step_4); break;
				case 5:	txt_interval.setText(Step_5); break;
				case 6:	txt_interval.setText(Step_6); break;
				case 7:	txt_interval.setText(Step_7); break;
				case 8:	txt_interval.setText(Step_8); break;
				case 9:	txt_interval.setText(Step_9); break;
				}
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				txt_interval.setText(getString(R.string.loc_interval_representation));
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				switch(progress)
				{
				case 0:	txt_interval.setText(Step_0); interval_loc=progress; break;
				case 1:	txt_interval.setText(Step_1); interval_loc=progress; break;
				case 2:	txt_interval.setText(Step_2); interval_loc=progress; break;
				case 3:	txt_interval.setText(Step_3); interval_loc=progress; break;
				case 4:	txt_interval.setText(Step_4); interval_loc=progress; break;
				case 5:	txt_interval.setText(Step_5); interval_loc=progress; break;
				case 6:	txt_interval.setText(Step_6); interval_loc=progress; break;
				case 7:	txt_interval.setText(Step_7); interval_loc=progress; break;
				case 8:	txt_interval.setText(Step_8); interval_loc=progress; break;
				case 9:	txt_interval.setText(Step_9); interval_loc=progress; break;
				}
			}
		});
	    
	    //btn_showMap OnClickListener
        btn_showMap.setOnClickListener(new View.OnClickListener() 
        {
        	AlertDialog alert = Infodialog.create();
        	public void onClick(View arg0) {
        		// TODO Auto-generated method stub
        		alert.show();
        	}
        });
        
        btn_closeApp.setOnClickListener(new View.OnClickListener() 
        {
        	
        	public void onClick(View arg0) {
        		// TODO Auto-generated method stub
        		AlertDialog.Builder dialog = new AlertDialog.Builder(alertContext);
        		dialog.setMessage("Are you sure to exit this application?")
        			.setCancelable(false)
        			.setPositiveButton("Exit all", new DialogInterface.OnClickListener() {
        		           public void onClick(DialogInterface dialog, int id) {
        		        	   //Close the app
        		                startup.this.finish();
        		               //Close everything, also the Service
        		                stopService(serviceIntent);
        		                }
        		       })
        		       .setNeutralButton("Exit", new DialogInterface.OnClickListener() {
        		           public void onClick(DialogInterface dialog, int id) {
        		                startup.this.finish();
        		           }
        		       })
        			.setNegativeButton("No", new DialogInterface.OnClickListener() {
        				  public void onClick(DialogInterface dialog, int id) {
        					  	dialog.cancel();
        				  }
        		});
        		AlertDialog alert = dialog.create(); 
        		alert.show();
        	}
        });
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    private void handleButtonClick()
    {
    	startService(serviceIntent);
    	if (use_map)
    	{
    		startActivity(mapViewIntent);
    	}
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menue, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.home:
        	setContentView(R.layout.main_layout);
            return true;
        case R.id.about:
        	setContentView(R.layout.about_view);
            return true;
        case R.id.showMap:
        	startActivity(mapViewIntent);
        	return true;
        case R.id.exit:
        	AlertDialog.Builder dialog = new AlertDialog.Builder(alertContext);
    		dialog.setMessage("Are you sure to exit this application?")
    			.setCancelable(false)
    			.setPositiveButton("Exit all", new DialogInterface.OnClickListener() {
    		           public void onClick(DialogInterface dialog, int id) {
    		        	   //Close the app
    		                startup.this.finish();
    		               //Close everything, also the Service
    		                stopService(serviceIntent);
    		                }
    		       })
    		       .setNeutralButton("Exit", new DialogInterface.OnClickListener() {
    		           public void onClick(DialogInterface dialog, int id) {
    		                startup.this.finish();
    		           }
    		       })
    			.setNegativeButton("No", new DialogInterface.OnClickListener() {
    				  public void onClick(DialogInterface dialog, int id) {
    					  	dialog.cancel();
    				  }
    		});
    		AlertDialog alert = dialog.create(); 
    		alert.show();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

